import 'package:flutter/material.dart';
import 'package:kxy_flutter_app/events/events.dart';
import 'package:kxy_flutter_app/model/integral_rank.dart';
import 'package:kxy_flutter_app/view/my/integral/user_rank_item_widget.dart';

import 'dept_rank_item_widget.dart';

class MyIntegralRankWidget extends StatefulWidget {
  MyIntegralRankWidget({Key key}) : super(key: key);

  @override
  _MyIntegralRankWidgetState createState() => _MyIntegralRankWidgetState();
}

class _MyIntegralRankWidgetState extends State<MyIntegralRankWidget> {
  IntegralRank rank;
  int type;

  @override
  void initState() {
    super.initState();
    Events.instance.on<MyIntegralRankChanged>().listen((event) {
      setState(() {
        this.rank = event.rank;
        this.type = event.type;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    if (rank == null) {
      return Container(
        height: 90,
        margin: EdgeInsets.only(bottom: 5),
      );
    }

    Widget myWidget;
    if (type == 0) {
      myWidget = UserRankItemWidget(
        rankItem: new IntegralRankItem(
          realName: rank.realName,
          deptName: rank.deptName,
          headUrl: rank.headUrl,
          rank: rank.userRank,
          grade: rank.grade,
        ),
        isLight: true,
      );
    } else {
      myWidget = DeptRankItemWidget(
        rankItem: new IntegralRankItem(
          realName: rank.realName,
          deptName: rank.deptName,
          headUrl: rank.headUrl,
          rank: rank.userRank,
          avgGrade: rank.avgGrade,
        ),
        isLight: true,
      );
    }

    return Container(
      height: 90,
      child: myWidget,
      color: Colors.transparent,
      margin: EdgeInsets.only(bottom: 5),
    );
  }
}
